package models

import (
	"github.com/gin-gonic/gin"
	"gorm.io/gorm"
)

type Menu struct {
	gorm.Model
	Menuname    string `json:"menuname" form:"menuname" gorm:"type:varchar(20);not null unique"  binding:"required"`
	Children    string `json:"children" form:"children" gorm:"type:varchar(20);not null unique"  binding:"required"`
	SubChildren string `json:"subchildren" form:"subchildren" gorm:"type:varchar(20);not null unique"  binding:"required"`
	Path        string `json:"path" form:"path" gorm:"type:varchar(20);not null unique"  binding:"required"`
	SubPath     string `json:"subpath" form:"subpath" gorm:"type:varchar(20);not null unique"  binding:"required"`
}

// 获取单个菜单项
func GetMenu(c *gin.Context) (Menu, error) {
	json := make(map[string]interface{})
	c.ShouldBind(&json)
	menuname := json["menuname"]

	var menu Menu
	err := db.Table("menus").Where("menuname = ?", menuname).First(&menu).Error
	return menu, err
}

// 获取所有菜单项
func GetAllMenu(c *gin.Context) ([]Menu, error) {
	var menudata []Menu
	err := db.Table("menus").Find(&menudata).Error
	return menudata, err
}
